<?php
namespace app\controller\admin\system\admin;


use app\BaseController;
use app\common\repositories\system\AdminRepository;
use basic\services\CaptchaService;
use think\App;

class Login extends BaseController
{
    protected $repository;

    public function __construct(App $app, AdminRepository $repository)
    {

        parent::__construct($app);
        $this->repository = $repository;
    }

    public function login()
    {
        $data = $this->request->params(['account', 'password', 'code', 'key']);
        /*if(!CaptchaService::check($data['code'],$data['key'])){
            return fail('验证码错误');
        }*/

        $adminInfo = $this->repository->login($data['account'], $data['password']);
        $tokenInfo = $this->repository->createToken($adminInfo);
        $admin = $adminInfo->toArray();
        unset($admin['pwd']);
        $data = [
            'token' => $tokenInfo['token'],
            'exp' => $tokenInfo['out'],
            'admin' => $admin
        ];

        return app('json')->success($data);
    }


    public function logout()
    {
        if ($this->request->isLogin())
            $this->repository->clearToken($this->request->token());
        return app('json')->success('退出登录');
    }

    public function getCaptcha()
    {
        return success(CaptchaService::create(true));
    }

}
